if not TUNING.NPC_CONFIG.SKELETON_EVENT then
    return
end

AddPrefabPostInit(
    "skeleton",
    function(inst)

        inst:AddTag("npc_tag.skeleton")


        if not TheWorld.ismastersim then
            return
        end
        if inst.components.npc_base_lib == nil then
            inst:AddComponent("npc_base_lib")
        end
        if inst.components.npc_everything_data == nil then
            inst:AddComponent("npc_everything_data")
        end

        
        if inst.components.workable then
            inst:DoTaskInTime(0.1,function()
                inst.components.workable:SetWorkLeft(3)
                inst.components.workable.onwork___npc_old = inst.components.workable.onwork
                inst.components.workable.onwork = function(inst,worker,...)
                    if worker and worker:HasTag("player") and worker.components.playercontroller and worker.npc_everything_data then
                        local random_num = inst.npc_everything_data:Get("skeleton.ready") or math.random(1000)
                        if worker.npc_everything_data:Get("skeleton_event.first") ~= true then
                            random_num = 100
                            worker.npc_everything_data:Set("skeleton_event.first",true)
                        end
                        if random_num <= 200 then
                            local pt = Vector3(inst.Transform:GetWorldPosition())
                            SpawnPrefab("collapse_big").Transform:SetPosition(inst.Transform:GetWorldPosition())  --- 烟雾
                            local npc = SpawnPrefab("wilton_npc")
                            npc.Ready = true
                            npc.__link_player = inst:GetNearestPlayer(true)
                            npc.Transform:SetPosition(pt.x,0,pt.z)
                            npc:AddDebuff("npc_event_trick_or_treat_from_wilton","npc_event_trick_or_treat_from_wilton")
                            npc.npc_base_lib:Throw_Out_Items({
                                    name = "boneshard",
                                    num = math.random(3, 8),
                                    range = 4,
                                    height = 3,
                                    random_height = true
                            })
                            inst.components.workable.onfinish = nil
                            inst.components.workable.workleft = 10
                            inst:Remove()
                            return
                        end
                    end
                    if inst.components.workable.onwork___npc_old ~= nil then
                        inst.components.workable.onwork___npc_old(inst,worker,...)
                    end
                end



            end)
        end  
        

        inst:ListenForEvent("skeleton.ready",function()
            inst.npc_everything_data:Set("skeleton.ready",10)
        end)
        inst:ListenForEvent("skeleton.ready2",function()
            inst.npc_everything_data:Set("skeleton.ready",nil)
        end)
    end
)



AddPrefabPostInit(
    "skeleton_player",
    function(inst)

        inst:AddTag("npc_tag.skeleton")


        if not TheWorld.ismastersim then
            return
        end
        if inst.components.npc_base_lib == nil then
            inst:AddComponent("npc_base_lib")
        end
        if inst.components.npc_everything_data == nil then
            inst:AddComponent("npc_everything_data")
        end

        
        if inst.components.workable then
            inst:DoTaskInTime(0.1,function()
                inst.components.workable:SetWorkLeft(3)
                inst.components.workable.onwork___npc_old = inst.components.workable.onwork
                inst.components.workable.onwork = function(inst,worker,...)
                    if worker and worker:HasTag("player") and worker.components.playercontroller 
                    and inst.char == worker.prefab and inst.userid == worker.userid
                    and worker.npc_everything_data then
                        local random_num = math.random(1000)
                        if worker.npc_everything_data:Get("skeleton_event.player.first") ~= true then
                            random_num = 100
                            worker.npc_everything_data:Set("skeleton_event.player.first",true)
                        end
                        if random_num <= 200 then
                            local pt = Vector3(inst.Transform:GetWorldPosition())
                            SpawnPrefab("collapse_big").Transform:SetPosition(inst.Transform:GetWorldPosition())  --- 烟雾
                            local npc = SpawnPrefab("wilson_npc")
                            npc.components.skinner:CopySkinsFromPlayer(worker)
                            npc.AnimState:SetMultColour(1,1,1,0.7)

                            npc.Ready = true
                            npc.__link_player = worker
                            -- npc.__link_player = inst:GetNearestPlayer(true)
                            npc.Transform:SetPosition(pt.x,0,pt.z)
                            npc:AddDebuff("npc_event_trick_or_treat_from_wilton","npc_event_trick_or_treat_from_wilton")
                            npc.npc_base_lib:Throw_Out_Items({
                                    name = "boneshard",
                                    num = math.random(3, 8),
                                    range = 4,
                                    height = 3,
                                    random_height = true
                            })
                            inst.components.workable.onfinish = nil
                            inst.components.workable.workleft = 10
                            inst:Remove()
                            return
                        end
                    end
                    if inst.components.workable.onwork___npc_old ~= nil then
                        inst.components.workable.onwork___npc_old(inst,worker,...)
                    end
                end



            end)
        end  
        
    end
)